package com.twt.controller;

import com.twt.Dao.MyLakeDao;
import com.twt.entity.Result;
import com.twt.entity.User;
import com.twt.service.MyLakeService;
import com.twt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;



@RestController
@RequestMapping("/myLake")
public class MyLakeController {
    @Autowired
    private MyLakeService myLakeService;
    @Autowired
    private UserService userService;
    @Autowired
    private MyLakeDao myLakeDao;

    /**
     * 直接把user类返回 即用户的所有信息
     *
     * @param username 参数封装
     * @return Result
     */
    @GetMapping("/initial")
    public Result myLake(@RequestParam("username") String username) {
        Result result = new Result();
        result.setMsg("用户查询成功");
        User one= myLakeDao.getUser(username);
        if(one.getHead() != null)
        one.setHead("http://60.205.223.228:8080/images/"+one.getHead());
        result.setDetail(one);
        result.setSuccess(true);
        return result;

    }

    /**
     * 修改密码
     * 需要判断用户原始密码是否正确,否则返回“原始密码输入错误”。然后判断两次新密码是否一致,否则返回“两次密码输入不一致”
     * 都没问题的话返回 “密码修改成功”
     *
     * @param username,oldPassword,newPassword1,newPassword2 参数封装
     * @return Result
     */

    @PostMapping("/changePassword")
    public Result changePassword(@RequestParam("username") String username, @RequestParam("oldPassword") String
            oldPassword, @RequestParam("newPassword1") String newPassword1, @RequestParam("newPassword2") String
                                         newPassword2) {
        return myLakeService.changePassword(username, oldPassword, newPassword1, newPassword2);
    }

    /**
     * 修改昵称
     * *除了user表，不能忘了改所有瓶子表中的nickname
     *
     * @param username,nickname 参数封装
     * @return Result
     */

    @PostMapping("/changeNickName")
    public Result changeNickName(@RequestParam("nickname") String nickname, @RequestParam("username") String
            username) {
        return myLakeService.changeNickname(username, nickname);
    }



    /**
     * 头像自定义上传
     *
     * @param username,file 参数封装
     * @return Result
     */
    @PostMapping(value = "/setHead")
    public Result setHead(@RequestParam("file") MultipartFile multipartFile, @RequestParam("username") String
            username) {
        return myLakeService.setHead(multipartFile, username);
    }

    /**
     * 更改系统头像
     *
     * @param username,firstHead 参数封装
     * @return Result
     */
    @PostMapping(value = "/setFirstHead")
    public Result setFirstHead(@RequestParam("firstHead") int firstHead, @RequestParam("username") String username) {
        return userService.setFirstHead(firstHead, username);
    }



    /**
     * 用户注销
     * 一是要删除user表中的所有信息，其次要删瓶子，lake里面他的瓶子要被删掉，
     * 所有他之前的瓶子被别人捡起来之后，表中exist置false，他捞起来的瓶子，表中exist置false
     *
     * @param username 参数封装
     * @return Result
     */
    @PostMapping("/shutDown")
    public Result shutDown(@RequestParam("username") String username) {
        return myLakeService.shutDown(username);
    }



}
